package com.ysd.lis.controller.bac;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.bac.BacTestResult;
import com.ysd.lis.entity.lab.LabCriticalReportDto;
import com.ysd.lis.entity.lab.LabMaininfo;
import com.ysd.lis.entity.lab.LabRptresult;
import com.ysd.lis.request.bac.BacDrugDto;
import com.ysd.lis.service.bac.BacTestResultService;
import com.ysd.lis.service.lab.LabMaininfoService;
import com.ysd.lis.service.lab.LabRptresultService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cfz
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/api/bac/bac-test-result")
public class BacTestResultController extends BaseController {
    @Autowired
    BacTestResultService bacTestResultService;
    @Autowired
    LabMaininfoService labMaininfoService;
    @Autowired
    LabRptresultService labRptresultService;

    @ApiOperation("查询")
    @GetMapping("/findBacTestResultList")
    public Result findBacTestResultList(BacTestResult bacTestResult) {
        return bacTestResultService.findBacTestResultList(bacTestResult);
    }

    @ApiOperation("获取是危急值的细菌结果")
    @GetMapping("/getAlarmResult")
    public Result getAlarmResult(LabCriticalReportDto labCriticalReportDto) {
        return bacTestResultService.getAlarmResult(labCriticalReportDto);
    }

    @ApiOperation("查询细菌历史回顾")
    @GetMapping("/findTestResultHistoryList")
    public Result findTestResultHistoryList(BacTestResult bacTestResult) {
        return bacTestResultService.findTestResultHistoryList(bacTestResult);
    }

    @ApiOperation("增加")
    @PostMapping("/addBacTestResult")
    public Result addBacTestResult(@RequestBody BacTestResult bacTestResult) {
        return bacTestResultService.addBacTestResult(bacTestResult);
    }

    @ApiOperation("删除")
    @PostMapping("/deleteBacTestResult")
    public Result deleteBacTestResult(@RequestBody BacTestResult bacTestResult) {
        return bacTestResultService.deleteBacTestResult(bacTestResult);
    }

    @ApiOperation("修改")
    @PostMapping("/updateBacTestResult")
    public Result updateBacTestResult(@RequestBody BacTestResult bacTestResult) {
        return bacTestResultService.updateBacTestResult(bacTestResult);
    }

    @ApiOperation("通过病历号查询出检验结果")
    @GetMapping("/findLabRptresultByPatNo")
    public Result findLabRptresultByPatNo(String patNo) {
        LambdaQueryWrapper<LabMaininfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LabMaininfo::getPatNo, patNo)
                .eq(LabMaininfo::getDelFlag, 0);
        List<LabMaininfo> list = labMaininfoService.list(lambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            List<String> collect = list.stream().map(LabMaininfo::getId).collect(Collectors.toList());
            LambdaQueryWrapper<LabRptresult> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.in(LabRptresult::getMaininfoId, collect)
                    .eq(LabRptresult::getDelFlag, 0);
            List<LabRptresult> list1 = labRptresultService.list(lambdaQueryWrapper1);
            return Result.succ(1, "查询成功", list1);
        }
        return Result.succ(1, "查询成功", new ArrayList<>());
    }

    @ApiOperation("查询细菌药敏数据")
    @GetMapping("/findTestResultAndDrugList")
    public Result findTestResultAndDrugList(BacTestResult bacTestResult) {
        return bacTestResultService.findTestResultAndDrugList(bacTestResult);
    }

    @ApiOperation("同步细菌药敏数据")
    @PostMapping("/syncTestResultAndDrugList")
    public Result syncTestResultAndDrugList(@RequestBody BacDrugDto dto) {
        return bacTestResultService.syncTestResultAndDrugList(dto);
    }

}
